package com.zhihaoliang.coroutine.buss.net.ui

import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.zhihaoliang.coroutine.R
import com.zhihaoliang.coroutine.base.BaseActivity
import com.zhihaoliang.coroutine.buss.net.adapter.UserAdapter
import com.zhihaoliang.coroutine.buss.net.callback.IUserLoadData
import com.zhihaoliang.coroutine.databinding.NetActivityBinding

/**
 * @author 支豪亮
 * @date 2022/10/20 09:57
 * @des 用来展示网络操作
 */
class NetActivity : BaseActivity<NetActivityBinding>() {

    private val viewModel: NetViewModel by viewModels()

    private val userAdapter = UserAdapter()

    override fun initDataBinding(): NetActivityBinding = NetActivityBinding.inflate(layoutInflater)

    override fun bindData() {
        super.bindData()

        dataBinding.viewModel = viewModel
        dataBinding.loadData = IUserLoadData {
            viewModel.showUseTime()
            userAdapter.setData(it)
        }
    }

    override fun initView() {
        super.initView()

        initRecyclerView()
        initSpinner()
    }

    private fun initRecyclerView() {
        val gridLayoutManager = GridLayoutManager(this, 3)
        dataBinding.rcyCoroutine.layoutManager = gridLayoutManager
        dataBinding.rcyCoroutine.adapter = userAdapter
    }

    private fun initSpinner() {
        val list = listOf("线程 execute 阻塞", "异步 enqueue 回调", "协成加载", "协成并行", "协成加载进度", "协成通道")
        val adapter: ArrayAdapter<String> = ArrayAdapter(this, R.layout.net_layout_item_select,
                                                         list)
        adapter.setDropDownViewResource(R.layout.net_layout_item_select)
        dataBinding.spinner.prompt = "加载方式"
        dataBinding.spinner.adapter = adapter
        dataBinding.spinner.setSelection(viewModel.spinnerSelIndex)

        dataBinding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                viewModel.setSpinnerIndex(p2)
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }
    }

}